
package com.yxgc.chat.wildfire.reactlib;

import android.util.Log;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.yxgc.chat.wildfire.nativelib.ChatHelper;

import java.util.List;

public class ChatModule extends ReactContextBaseJavaModule {

    private final ReactApplicationContext reactContext;

    public ChatModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }


    @ReactMethod
    public void addNativeListener(String eventName) {
        // Set up any upstream listeners or background tasks as necessary
    }

    @ReactMethod
    public void removeNativeListeners(Integer count) {
        // Remove upstream listeners, stop unnecessary background tasks
    }

    @Override
    public String getName() {
        return "ImModule";
    }

    /**
     * 初始化
     */
    @ReactMethod
    public void IMInit(String url) {
        ChatHelper.getInstance(reactContext).chatInit(reactContext, url);
    }

    /**
     * 连接
     * */
    @ReactMethod
    public void ImConnect(String id, String token, Callback callback) {
        /**
         * client id 和token 强依赖，-6就是token 和client id 不匹配，修改ChatManager  911行 imei
         * */
        boolean isConnect = ChatHelper.getInstance(reactContext).chatConnect(id, token);
        callback.invoke(isConnect);
    }


    /**
     * 获取会话列表
     * */
    @ReactMethod
    public void getConversionList(Callback successCallback, Callback errorCallback) {
        ChatHelper.getInstance(reactContext).getConversionList(successCallback, errorCallback);
    }

    /**
     * 获取未读消息
     * */
    @ReactMethod
    public void getUnReadStatus(Callback successCallback, Callback errorCallback) {
        ChatHelper.getInstance(reactContext).reloadConversationUnreadStatus(successCallback, errorCallback);
    }

    /**
     * 获取联系人列表
     * */
    @ReactMethod
    public void getContractList(Callback callback) {
        ChatHelper.getInstance(reactContext).getFriendList(callback);
    }

    /**
     * 添加好友
     * */
    @ReactMethod
    public void addFriend(String userId, String reason, Callback callback) {
        ChatHelper.getInstance(reactContext).addFriend(userId, reason, callback);
    }

    /**
     * 获取好友申请
     * */
    @ReactMethod
    public void getFriendRequest(Callback callback) {
        ChatHelper.getInstance(reactContext).getFriendRequest(callback);
    }
    /**
     * 同意好友申请
     * */
    @ReactMethod
    public void acceptFriendRequest(String userId, Callback callback) {
        Log.e("yxgc", "LoadWildFireConversion");
        ChatHelper.getInstance(reactContext).acceptFriendRequest(userId, callback);
    }

    /**
     * 获取会话标题
     * */
    @ReactMethod
    public void getConversionTitle(String target, String conversionType, Callback callback) {
        ChatHelper.getInstance(reactContext).getConversionTitle(target, conversionType, callback);
    }
    /**
     * 获取会话消息
     * */
    @ReactMethod
    public void getConversionMessage(String conversion, Callback callback, Callback errBack) {
        ChatHelper.getInstance(reactContext).getMessage(conversion, callback, errBack);
    }

    /**
     * 发送文字消息
     * */
    @ReactMethod
    public void sendTxt(String conversation, String content, Callback callback) {
        ChatHelper.getInstance(reactContext).sendTxt(conversation, content, callback);
    }

    /**
     * 发送视频消息
     * */
    @ReactMethod
    public void sendVideo(String conversation, String filePath, Callback c) {
        ChatHelper.getInstance(reactContext).sendVideo(conversation, filePath, c);
    }

    /**
     * 发送语音消息
     * */
    @ReactMethod
    public void sendAudio(String conversation, String audioFilePath, int duration, Callback callback) {
        ChatHelper.getInstance(reactContext).sendAudio(conversation, audioFilePath, duration, callback);
    }
    /**
     * 发送图片消息
     * */
    @ReactMethod
    public void sendImg(String conversation, String imageFileThumbPath, String imageFileSourcePath, Callback callback) {
        ChatHelper.getInstance(reactContext).sendImg(conversation, imageFileThumbPath, imageFileSourcePath, callback);
    }



    /**
     * 创建群聊
     * */
    @ReactMethod
    public void createGroup(String groupName, List<String> selectedIds, Callback callback) {
        ChatHelper.getInstance(reactContext).createGroup(groupName, selectedIds, callback);
    }

    /**
     * 修改群名称
     */
    public void modifyGroupName(String groupId, int modifyType, String newValue, Callback callback) {
        ChatHelper.getInstance(reactContext).modifyGroupName(groupId, modifyType, newValue, callback);
    }

    /**
     * 退出群聊
     */
    public void quiteGroup(String groupId, Callback callback) {
        ChatHelper.getInstance(reactContext).quiteGroup(groupId, callback);
    }

    /**
     * 删除好友
     */
    public void deleteFriend(String userId, Callback callback) {
        ChatHelper.getInstance(reactContext).deleteFriend(userId, callback);
    }

    /**
     * 设置用户昵称
     */
    public void setFriendAlias(String userId, String alias, Callback callback) {
        ChatHelper.getInstance(reactContext).setFriendAlias(userId, alias, callback);
    }

    /**
     * 获取用户昵称
     */
    public void getFriendAlias(String userId, Callback callback) {
        ChatHelper.getInstance(reactContext).getFriendAlias(userId, callback);
    }

}